uniform extern float4x4 gWorld;
uniform extern float4x4 gViewProj;
uniform extern texture  gTex;
uniform extern texture  gDepthTex;

sampler Tex = sampler_state
{
	Texture = <gTex>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = POINT;
	AddressU  = CLAMP;
    AddressV  = CLAMP;
};

sampler DepthTex = sampler_state
{
	Texture = <gDepthTex>;
	MinFilter = POINT;
	MagFilter = POINT;
	MipFilter = POINT;
	AddressU  = CLAMP;
    AddressV  = CLAMP;
};


struct VSOutput
{
	float4 posH : POSITION0;
	float4 readablePosH : TEXCOORD0;
	float2 texCoord : TEXCOORD1;
};

VSOutput GlowExtractVS(float3 posL : POSITION0,
					   float2 texCoord : TEXCOORD0)
{
	VSOutput output = (VSOutput)0;
	
	output.posH = mul(float4(posL, 1.0f), mul(gWorld, gViewProj));
	output.readablePosH = output.posH;
	output.texCoord = texCoord;
	
	return output;
}

void GlowExtractPS(VSOutput input, out float4 color : COLOR0)
{
	color = float4( tex2D(Tex, input.texCoord) );
	color.a = input.readablePosH.z / input.readablePosH.w;
	//float2 depthTexCoord = float2(input.texCoord.x * 0.5f + 0.5f, input.texCoord.y * 0.5f + 0.5f);
	//depth = tex2D(DepthTex, depthTexCoord).r;
	//if( (input.posH2.z / input.posH2.w) < depth )
	//{
	//	color = float4(1,0,0,1);
	//}
	//color = depth;
}

technique GlowExtract
{
    pass P0
    {
        AlphaBlendEnable = false;
        ZEnable = true;
		ZWriteEnable = true;

        VertexShader = compile vs_1_1 GlowExtractVS();
        PixelShader = compile ps_2_0 GlowExtractPS();
    }
}
